package org.jaaksi.customviewdemo.view

import android.content.Context
import android.graphics.Canvas
import android.graphics.Color
import android.graphics.Paint
import android.graphics.Rect
import android.util.AttributeSet
import android.view.View

class TestDrawView(context: Context?, attrs: AttributeSet?) : View(context, attrs) {
    private var mPaint: Paint = Paint()
    private var rect = Rect()
    private var clipRect = Rect()

    init {
        //设置画笔style
        mPaint.style = Paint.Style.FILL
        mPaint.color = Color.RED
        mPaint.strokeWidth = 10f
    }

    override fun onSizeChanged(w: Int, h: Int, oldw: Int, oldh: Int) {
        super.onSizeChanged(w, h, oldw, oldh)
        rect.set(w / 2, h / 2 - 10, w, h / 2 + 10)
        clipRect.set(w / 2, 0, w - 100, h)
    }

    override fun onDraw(canvas: Canvas) {
        super.onDraw(canvas)
        val clipId = canvas.save()
        mPaint.color = Color.RED
        // 中心点线
        canvas.drawLine(width / 2f, 0f, width / 2f, height.toFloat(), mPaint)
        // 距右边100px线条
        canvas.drawLine(width - 100f, 0f, width - 100f, height.toFloat(), mPaint)
//        canvas.translate(-100f, 0f)
        canvas.clipRect(clipRect)
//        canvas.restoreToCount(clipId)
        mPaint.color = Color.RED
        canvas.drawRect(rect, mPaint)
        mPaint.color = Color.BLUE
//        canvas.drawLine(width / 2f, 0f, width / 2f, height.toFloat(), mPaint)
        canvas.restoreToCount(clipId)
//        canvas.drawLine(width / 2f, 0f, width / 2f, height.toFloat(), mPaint)

    }
}